package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.*;
import com.atguigu.tingshu.model.dto.BaseCategoryViewDto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;
	@Resource
	BaseCategoryViewMapper baseCategoryViewMapper;
	@Resource
	BaseAttributeMapper baseAttributeMapper;
	@Resource
	BaseAttributeValueMapper baseAttributeValueMapper;

	/**
	 * [
	 *  {
	 *     "categoryName":"图书、音像、电子书刊",   #一级分类名称
	 *     "categoryId":1,                       #一级分类ID
	 *     "categoryChild":[                     #当前一级分类包含的二级分类集合
	 *         {
	 *             "categoryName":"电子书刊",     #二级分类名称
	 *             "categoryId":1,               #二级分类ID
	 *             "categoryChild":[             #当前二级分类包含的三级分类集合
	 *                 {
	 *                     "categoryName":"电子书",#三级分类名称
	 *                     "categoryId":1         #三级分类ID
	 *                 },
	 * @return
	 */
	@Override
	public List<BaseCategoryViewDto> getBaseCategoryList() {
		//一级分类集合
		List<BaseCategory1> baseCategory1List = baseCategory1Mapper.selectList(null);
		List<BaseCategoryViewDto> list1=new ArrayList<>();
		baseCategory1List.stream().forEach(baseCategoryView1 -> {
			BaseCategoryViewDto baseCategoryViewDto1=new BaseCategoryViewDto();
			baseCategoryViewDto1.setCategoryId(baseCategoryView1.getId());
			baseCategoryViewDto1.setCategoryName(baseCategoryView1.getName());

			//二级分类的父ID为一级分类的集合
			List<BaseCategory2> baseCategory2List1 = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, baseCategoryView1.getId()));
			List<BaseCategoryViewDto> list2=new ArrayList<>();
			baseCategory2List1.stream().forEach(baseCategory2 -> {
				BaseCategoryViewDto baseCategoryViewDto2=new BaseCategoryViewDto();
				baseCategoryViewDto2.setCategoryId(baseCategory2.getId());
				baseCategoryViewDto2.setCategoryName(baseCategory2.getName());
			//根据二级分类id查找3级分类的集合
				List<BaseCategory3> baseCategory3List1 = baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>().eq(BaseCategory3::getCategory2Id, baseCategory2.getId()));
				List<BaseCategoryViewDto> list3=new ArrayList<>();
				baseCategory3List1.stream().forEach(baseCategory3 -> {
					BaseCategoryViewDto baseCategoryViewDto3=new BaseCategoryViewDto();
					baseCategoryViewDto3.setCategoryId(baseCategory3.getId());
					baseCategoryViewDto3.setCategoryName(baseCategory3.getName());
					list3.add(baseCategoryViewDto3);
				});
				baseCategoryViewDto2.setCategoryChild(list3);
				list2.add(baseCategoryViewDto2);
			});

			baseCategoryViewDto1.setCategoryChild(list2);
			list1.add(baseCategoryViewDto1);
		});
		return list1;
	}

	@Override
	public List<BaseAttribute> findAttributeByCategory1Id(Long category1Id) {
		List<BaseAttribute> list=new ArrayList<>();
		QueryWrapper<BaseAttribute> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("category1_id",category1Id);
		List<BaseAttribute> baseAttributeList = baseAttributeMapper.selectList(queryWrapper);
		baseAttributeList.stream().forEach(baseAttribute -> {
			baseAttribute.setCategory1Id(category1Id);
			baseAttribute.setAttributeName(baseAttribute.getAttributeName());
			List<BaseAttributeValue> list2=new ArrayList<>();
			QueryWrapper<BaseAttributeValue> queryWrapper1=new QueryWrapper<>();
			queryWrapper1.eq("attribute_id",baseAttribute.getId());
			List<BaseAttributeValue> baseAttributeValueList = baseAttributeValueMapper.selectList(queryWrapper1);
			baseAttributeValueList.stream().forEach(baseAttributeValue -> {
				baseAttributeValue.setAttributeId(baseAttributeValue.getAttributeId());
				baseAttributeValue.setValueName(baseAttributeValue.getValueName());
				list2.add(baseAttributeValue);
			});
			baseAttribute.setAttributeValueList(list2);
			list.add(baseAttribute);
		});

		return list;
	}
}
